package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.ChargeProject;
import com.kkb.service.ChargeProjectService;
import com.kkb.vo.ChargeProjectQueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("chargeProject")
public class ChargeProjectController {

    @Resource
    ChargeProjectService service;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO queryAll(ChargeProjectQueryVO vo) {
        return new ResultVO(service.queryAll(vo));
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO queryById(@PathVariable("id") int id) {
        ChargeProject chargeProject = service.queryById(id);
        return chargeProject == null ? new ResultVO<>(404, "未找到资源") : new ResultVO<>(chargeProject);
    }

    @RequestMapping(value = "page",method = RequestMethod.GET)
    public ResultVO<ChargeProject> selectInPage(Integer index, Integer size, String name){
        if ("".equals(name.trim())){
            name=null;
        }
        PageInfo<ChargeProject> chargeProjectPageInfo = service.selectInPage(index, size, name);
        return chargeProjectPageInfo != null ?
                new ResultVO<ChargeProject>(chargeProjectPageInfo): new ResultVO<>(404,"暂无数据");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO deleteChargeProject(@PathVariable("id") Integer id){
        return service.deleteById(id) > 0 ? new ResultVO() : new ResultVO(500,"删除失败，服务器异常");
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public ResultVO updateChargeProject(ChargeProject chargeProject){
        return service.updateChargeProject(chargeProject) > 0 ?
                new ResultVO(): new ResultVO(500,"更新失败，服务器异常");
    }
}
